package com.yuwj.chinaa.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.yuwj.chinaa.R;
import com.yuwj.chinaa.adapter.TradeFragmentBuyAdapter;
import com.yuwj.chinaa.adapter.TradeFragmentSaleAdapter;
import com.yuwj.chinaa.base.BaseActivity;
import com.yuwj.chinaa.base.ConstantParam;
import com.yuwj.chinaa.entity.EntityOrderWithTrade;
import com.yuwj.chinaa.entity.EntityOrderWithTrade;
import com.yuwj.chinaa.presenter.IPresenter;
import com.yuwj.chinaa.presenter.presenterImpl.PresenterImpl;
import com.yuwj.chinaa.util.ToastUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TradeListActivity extends BaseActivity {


    @BindView(R.id.iv_back)
    ImageButton ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.srl_list)
    SwipeRefreshLayout srlList;
    int currentTradeListType = ConstantParam.TRANSACTION_TYPE_WITH_BUY;//当前交易列表数据类型
    List<EntityOrderWithTrade> entityTradeSaleList;//出售列表
    List<EntityOrderWithTrade> entityTradeBuyList;//求购列表
    IPresenter presenter;
    TradeFragmentSaleAdapter tradeFragmentSaleAdapter;
    TradeFragmentBuyAdapter tradeFragmentBuyAdapter;
    @BindView(R.id.v_buy_list)
    View vBuyList;
    @BindView(R.id.ll_buy_list)
    LinearLayout llBuyList;
    @BindView(R.id.v_sell_list)
    View vSellList;
    @BindView(R.id.ll_sell_list)
    LinearLayout llSellList;

    public static Intent createIntent(Context context) {
        return new Intent(context, TradeListActivity.class);
    }

    @Override
    public int setCustomContentViewResourceId() {
        return R.layout.activity_trade_list;
    }

    @Override
    public void initParam() {
        presenter = new PresenterImpl(this);
        entityTradeSaleList = new ArrayList<>();
        entityTradeBuyList = new ArrayList<>();
        tradeFragmentSaleAdapter = new TradeFragmentSaleAdapter(context, entityTradeSaleList);
        tradeFragmentBuyAdapter = new TradeFragmentBuyAdapter(context, entityTradeBuyList);
    }

    @Override
    public void initView() {
        tvTitle.setText("交易订单");
        tradeFragmentBuyAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                EntityOrderWithTrade order = (EntityOrderWithTrade) adapter.getItem(position);
                if ((entityUser.getID() + "").equals(order.getUserId())) {
                    ToastUtil.getInstance().shortLength(context, "无法与自己交易");
                } else {
                    startActivity(TradeListDetailActivity.createIntent(context, order, currentTradeListType));
                }
            }
        });
        rvList.setAdapter(tradeFragmentBuyAdapter);
        rvList.setLayoutManager(new LinearLayoutManager(context));

        tradeFragmentSaleAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                EntityOrderWithTrade order = (EntityOrderWithTrade) adapter.getItem(position);
                if ((entityUser.getID() + "").equals(order.getUserId())) {
                    ToastUtil.getInstance().shortLength(context, "无法与自己交易");
                } else {
                    startActivity(TradeListDetailActivity.createIntent(context, order, currentTradeListType));
                }
            }
        });
        srlList.setEnabled(false);
    }

    @Override
    public void initOperator() {
        updateViewByCurrentTradeList();

    }


    @OnClick({R.id.iv_back, R.id.ll_buy_list, R.id.ll_sell_list})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_buy_list:
                currentTradeListType = ConstantParam.TRANSACTION_TYPE_WITH_BUY;
                updateViewByCurrentTradeList();
                break;
            case R.id.ll_sell_list:
                currentTradeListType = ConstantParam.TRANSACTION_TYPE_WITH_SELL;
                updateViewByCurrentTradeList();
                break;
        }
    }


    private void querySellAndBuyList() {
        JSONObject paramWithSaleBuy = new JSONObject();
        try {
            paramWithSaleBuy.put("pageindex", "1");
            paramWithSaleBuy.put("pagesize", "100");
            paramWithSaleBuy.put("ordertype", currentTradeListType == ConstantParam.TRANSACTION_TYPE_WITH_SELL ? 0 + "" : 1 + "");//交易订单类型：0：获取卖出订单，1：获取买入订单（交易中 订单不区分买入和卖出类型）
            paramWithSaleBuy.put("orderStatus", "0");//订单状态：0：等待交易，1：交易中 2：交易完成，3：申诉订单
            paramWithSaleBuy.put("isAll", "0");//是否查询所有：0：所有交易订单，1：个人交易订单

        } catch (JSONException e) {
            e.printStackTrace();
        }
        presenter.queryTransactionOrderList(paramWithSaleBuy.toString());
    }

    public void updateViewByCurrentTradeList() {
        switch (currentTradeListType) {
            case ConstantParam.TRANSACTION_TYPE_WITH_SELL:
                vBuyList.setVisibility(View.INVISIBLE);
                vSellList.setVisibility(View.VISIBLE);
                rvList.setAdapter(tradeFragmentSaleAdapter);
                querySellAndBuyList();

                break;
            case ConstantParam.TRANSACTION_TYPE_WITH_BUY:
                vBuyList.setVisibility(View.VISIBLE);
                vSellList.setVisibility(View.INVISIBLE);
                rvList.setAdapter(tradeFragmentBuyAdapter);
                querySellAndBuyList();
                break;
        }

    }


    @Override
    public void queryTransactionOrderListSuccess(List<EntityOrderWithTrade> list) {
        super.queryTransactionOrderListSuccess(list);
        if (list.size() > 0) {
            switch (currentTradeListType) {

                case ConstantParam.TRANSACTION_TYPE_WITH_SELL:
                    entityTradeSaleList.clear();
                    entityTradeSaleList.addAll(list);
                    tradeFragmentSaleAdapter.notifyDataSetChanged();
                    break;

                case ConstantParam.TRANSACTION_TYPE_WITH_BUY:
                    entityTradeBuyList.clear();
                    entityTradeBuyList.addAll(list);
                    tradeFragmentBuyAdapter.notifyDataSetChanged();
                    break;
            }
        }
    }

    @Override
    public void queryTransactionOrderListFail(Exception e) {
        super.queryTransactionOrderListFail(e);
        ToastUtil.getInstance().shortLength(context, e.getMessage());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
